babl: add babl_space_rgb_matrix
authorØyvind Kolås <pippin@gimp.org>
Sat, 19 Aug 2017 02:19:37 +0000 (04:19 +0200)
committerØyvind Kolås <pippin@gimp.org>
Sat, 19 Aug 2017 02:19:37 +0000 (04:19 +0200)
babl/babl-space.c
babl/babl-space.h
babl/babl.h

index 37c40ade9c04a5a5925bc8de020d4aeec2e42642..a4d695c4a020cff60c7c9329b64522dc610619c0 100644 (file)
@@ -143,6 +143,70 @@ babl_space (const char *name)
   return NULL;
 }
 
+const Babl *
+babl_space_rgb_matrix (const char *name,
+                       double rx, double gx, double bx,
+                       double ry, double gy, double by,
+                       double rz, double gz, double bz,
+                       const Babl *trc_red,
+                       const Babl *trc_green,
+                       const Babl *trc_blue)
+{
+  int i=0;
+  static BablSpace space;
+  space.instance.class_type = BABL_SPACE;
+  space.instance.id         = 0;
+
+  space.xr = rx;
+  space.yr = gx;
+  space.xg = bx;
+  space.yg = ry;
+  space.xb = gy;
+  space.yb = by;
+  space.xw = rz;
+  space.yw = gz;
+  space.pad = bz;
+  space.trc[0] = trc_red;
+  space.trc[1] = trc_green?trc_green:trc_red;
+  space.trc[2] = trc_blue?trc_blue:trc_red;
+
+  for (i = 0; space_db[i].instance.class_type; i++)
+  {
+    int offset = ((char*)&space_db[i].xr) - (char*)(&space_db[i]);
+    int size   = ((char*)&space_db[i].trc) + sizeof(space_db[i].trc) - ((char*)&space_db[i].xr);
+
+    if (memcmp ((char*)(&space_db[i]) + offset, ((char*)&space) + offset, size)==0)
+      {
+        return (void*)&space_db[i];
+      }
+  }
+  if (i >= MAX_SPACES-1)
+  {
+    babl_log ("too many BablSpaces");
+    return NULL;
+  }
+  /* transplany matrixes */
+  babl_space_compute_matrices (&space_db[i]);
+  memcpy (space.RGBtoXYZ, &space.xr, sizeof (space.RGBtoXYZ));
+  babl_matrix_invert (space.RGBtoXYZ, space.XYZtoRGB);
+
+  space_db[i]=space;
+  space_db[i].instance.name = space_db[i].name;
+  if (name)
+    sprintf (space_db[i].name, "%s", name);
+  else
+    sprintf (space_db[i].name, "space-%.4f,%.4f_%.4f,%.4f_%.4f_%.4f,%.4f_%.4f,%.4f_%s,%s,%s",
+                       rx, gx, bx,
+                       ry, gy, by,
+                       rz, gz, bz,
+             babl_get_name (space.trc[0]),
+             babl_get_name(space.trc[1]), babl_get_name(space.trc[2]));
+
+
+  return (Babl*)&space_db[i];
+}
+
+
 const Babl *
 babl_space_rgb_chromaticities (const char *name,
                                double wx, double wy,
index e387d4f12542daddbae55ca2c7674db8b76a2609..2b2a883440ff7734b77a45ec77bb847188d53512 100644 (file)
@@ -40,6 +40,8 @@ typedef struct
   double           xb;  // blue primary chromaticity
   double           yb;
 
+  double           pad; // for when the numbers represent a matrix
+
   const Babl      *trc[3];
   char             name[128];
 
index b635c001af6829368c30dc3e98a6c3ee07390414..31d58dce719f97d793921d8052e8bb4306463a6c 100644 (file)
@@ -106,7 +106,7 @@ const Babl * babl_trc_gamma (double gamma);
 const Babl * babl_space (const char *name);
 
 /**
- * babl_space_new:
+ * babl_space_rgb_chromaticities:
  *
  * Creates a new RGB matrix color space definition with the specified
  * white point wx, wy, primary chromaticities rx,ry,gx,gy,bx,by and
@@ -122,6 +122,21 @@ const Babl * babl_space_rgb_chromaticities (const char *name,
                                             const Babl *trc_green,
                                             const Babl *trc_blue);
 
+/**
+ * babl_space_rgb_chromaticities:
+ *
+ * Creates a new RGB matrix color space definition using a precomputed
+ * D50 adapted 3x3 matrix, as possibly read from an ICC profile.
+ */
+const Babl *
+babl_space_rgb_matrix (const char *name,
+                       double rx, double gx, double bx,
+                       double ry, double gy, double by,
+                       double rz, double gz, double bz,
+                       const Babl *trc_red,
+                       const Babl *trc_green,
+                       const Babl *trc_blue);
+
 const double * babl_space_get_rgbtoxyz (const Babl *space);
 void babl_space_to_xyz   (const Babl *space, const double *rgb, double *xyz);
 void babl_space_from_xyz (const Babl *space, const double *xyz, double *rgb);